#include "../../include/car_parts/baseplate.h"

// 底盘的一些函数实现
BasePlate::BasePlate()
{
    // 初始化一些默认值
    this->model = "SCOUT MINI";
    this->wheelbase = 451;
    this->track = 490;
    this->groundClearance = 115;
    this->turningRadius = 0;
    this->driveType = "四轮四驱";
    this->maxRange = 10;
    this->tires[0] =
        {"公路轮", 175};
    this->tires[1] =
        {"公路轮", 175};
    this->tires[2] =
        {"麦克纳姆轮", 175};
    this->tires[3] =
        {"麦克纳姆轮", 175};
}

void BasePlate::setId(const std::string &id)
{
    this->id = id;
}
void BasePlate::setModel(const std::string &model)
{
    this->model = model;
}
void BasePlate::setWheelbase(int wheelbase)
{
    this->wheelbase = wheelbase;
}

void BasePlate::setTrack(int track)
{
    this->track = track;
}

void BasePlate::setGroundClearance(int groundClearance)
{
    this->groundClearance = groundClearance;
}

void BasePlate::setTurningRadius(int turningRadius)
{
    this->turningRadius = turningRadius;
}

void BasePlate::setDriveType(const std::string &driveType)
{
    this->driveType = driveType;
}

void BasePlate::setMaxRange(int maxRange)
{
    this->maxRange = maxRange;
}

void BasePlate::setTire(int index, const std::string &modelT, int size)
{
    if (index >= 0 && index < 4)
    {
        tires[index].modelT = modelT;
        tires[index].size = size;
    }
}

// 屏幕输出
void BasePlate::print()
{
    std::cout << "**底盘模块**"
              << "\n底盘驱动形式: " << driveType << "\n底盘最小离地间隙(mm): " << groundClearance << "\n底盘编号:" << id << "\n最大行程(km): " << maxRange << "\n底盘型号:"
              << model << "\n轮胎1型号: " << tires[0].modelT << "\n轮胎1尺寸(mm): " << tires[0].size << "\n轮胎2型号: " << tires[1].modelT
              << "\n轮胎2尺寸(mm): " << tires[1].size << "\n轮胎3型号: " << tires[2].modelT << "\n轮胎3尺寸(mm): " << tires[2].size << "\n轮胎4型号: " << tires[3].modelT
              << "\n轮胎4尺寸(mm): " << tires[3].size << "\n轮距(mm): " << track << "\n最小转弯半径(m): " << turningRadius << "\n轴距(mm): " << wheelbase << std::endl;
}

// 本地文件保存
void BasePlate::save(const std::string &filename)
{
    // 打开一个输出文件流
    std::ofstream file(filename, std::ios::app);
    if (!file.is_open())
    {
        std::cout << "未找到文件！" << std::endl;
        return;
    }

    file << "," << driveType << "," << groundClearance << "," << id << "," << maxRange << ","
         << model << "," << tires[0].modelT << "," << tires[0].size << "," << tires[1].modelT
         << "," << tires[1].size << "," << tires[2].modelT << "," << tires[2].size << "," << tires[3].modelT
         << "," << tires[3].size << "," << track << "," << turningRadius << "," << wheelbase;

    // 关闭文件流
    file.close();
}